package com.quectel.pay.module.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.pay.module.order.constants.OrderConstants;
import com.quectel.pay.module.order.dao.PayOrderDao;
import com.quectel.pay.module.order.dto.PayOrderDto;
import com.quectel.pay.module.order.dto.request.InitOrderParams;
import com.quectel.pay.module.order.entity.PayOrderEntity;
import com.quectel.pay.module.order.service.PayOrderService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.OrderUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ryan
 * @date 2021-01-27 14:10:55
 */
@DubboService
public class PayOrderServiceImpl implements PayOrderService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PayOrderServiceImpl.class);

    @Autowired
    private PayOrderDao payOrderDao;


    @Override
    public String genOrderNo() {
        return OrderUtils.getOrderNo();
    }

    @Override
    public PayOrderDto selectByOutTradeNo(String outTradeNo) {
        PayOrderEntity orderEntity = payOrderDao.selectOne(new QueryWrapper<PayOrderEntity>().eq("out_trade_no", outTradeNo).last("limit 1"));
        return CopyUtils.copyObj(orderEntity, PayOrderDto.class);
    }

    @Override
    public PayOrderDto initOrderInfo(InitOrderParams initOrderParams) {
        Date now = new Date();
        PayOrderDto orderDto = CopyUtils.copyObj(initOrderParams, PayOrderDto.class);


        orderDto.setId(Snowflake.nextId());
        orderDto.setOutTradeNo(
                initOrderParams.getOutTradeNo() != null
                        ?
                        initOrderParams.getOutTradeNo()
                        :
                        OrderUtils.getOrderNo()
        );
        orderDto.setStatus(OrderConstants.Status.INIT.getStatusValue());
        orderDto.setTotalRefundFee(BigDecimal.valueOf(SystemConstants.ZERO));
        orderDto.setCreateTime(now);
        orderDto.setUpdateTime(now);

        return orderDto;
    }

    @Override
    public List<PayOrderDto> queryList(Map<String, Object> params) {
        List<PayOrderEntity> list = payOrderDao.queryList(params);
        return CopyUtils.copyList(list, PayOrderDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return payOrderDao.queryTotal(params);
    }

    @Override
    public Long save(PayOrderDto dto) {
        PayOrderEntity entity = CopyUtils.copyObj(dto, PayOrderEntity.class);
        entity.setId(Snowflake.nextId());
        payOrderDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PayOrderDto dto) {
        PayOrderEntity entity = CopyUtils.copyObj(dto, PayOrderEntity.class);
        payOrderDao.updateById(entity);
    }


    @Override
    public PayOrderDto selectById(Long id) {
        PayOrderEntity entity = payOrderDao.selectById(id);
        return CopyUtils.copyObj(entity, PayOrderDto.class);
    }

    @Override
    public void deleteById(Long id) {
        payOrderDao.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
